
package com.hex.ds.code.detection.publish.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hex.ds.code.detection.publish.dao.ScriptPublishListMapper;
import com.hex.ds.code.detection.common.base.PageResult;
import com.hex.ds.code.detection.publish.entity.ScriptPublishListEntity;
import com.hex.ds.code.detection.publish.service.IScriptPublishListService;
import com.hex.ds.code.detection.publish.service.IScriptPublishResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 脚本发布清单
 *
 * @author Wang zhihao
 * @date 2023-09-13 18:17:51
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class ScriptPublishListServiceImpl extends ServiceImpl<ScriptPublishListMapper, ScriptPublishListEntity> implements IScriptPublishListService {

    private final IScriptPublishResultService scriptPublishResultService;

    private final ScriptPublishListMapper scriptPublishListMapper;

    @Override
    public PageResult queryList(Page page, ScriptPublishListEntity scriptPublishList) {
        LambdaQueryWrapper<ScriptPublishListEntity> wrapper = beforeQuery(scriptPublishList);
        Page returnPage = baseMapper.selectPage(page, wrapper);
        List<ScriptPublishListEntity> scriptPublishListEntities = returnPage.getRecords();
        return new PageResult(scriptPublishListEntities, returnPage.getTotal());
    }

    /**
     * @Method queryListNoPage
     * @Param
     * @param scriptPublishList
     * @Return java.util.List<com.hex.ds.code.detection.script.publish.entity.ScriptPublishListEntity>
     * @Description 不分页查询
     * @Author Wang zhihao
     * @Date 2023/11/10 13:28
     * @Version V1.0
     */
    @Override
    public List<ScriptPublishListEntity> queryListNoPage(ScriptPublishListEntity scriptPublishList) {
        LambdaQueryWrapper<ScriptPublishListEntity> wrapper = beforeQuery(scriptPublishList);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * @Method queryBackupEarliest
     * @Param
     * @param taskId
     * @param clusterId
     * @param type
     * @Return java.util.List<com.hex.ds.code.detection.script.publish.entity.ScriptPublishListEntity>
     * @Description 获取最久的备份表信息
     * @Author Wang zhihao
     * @Date 2023/11/15 18:46
     * @Version V1.0
     */
    @Override
    public List<ScriptPublishListEntity> queryBackupEarliest(String taskId, String clusterId, String type) {
        return scriptPublishListMapper.queryBackupEarliest(taskId, clusterId, type);
    }

    /**
     * @Method removeByTaskId
     * @Param
     * @param taskId
     * @Return void
     * @Description 根据taskId删除
     * @Author Wang zhihao
     * @Date 2023/11/13 17:37
     * @Version V1.0
     */
    @Override
    public void removeByTaskId(String taskId) {
        List<ScriptPublishListEntity> scriptPublishListEntities = queryListNoPage(new ScriptPublishListEntity().setScriptPublishTaskId(taskId));
        List<String> listIds = scriptPublishListEntities.stream().map(ScriptPublishListEntity::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(listIds)) {
            removeByIds(listIds);
        }
    }

    /**
     * @Method removeByIds
     * @Param
     * @param listIds
     * @Return void
     * @Description 批量删除
     * @Author Wang zhihao
     * @Date 2023/11/13 17:37
     * @Version V1.0
     */
    @Override
    public void removeByIds(List<String> listIds) {
        for (String listId : listIds) {
            removeById(listId);
        }
    }

    /**
     * @Method removeById
     * @Param
     * @param listId
     * @Return void
     * @Description 根据id删除
     * @Author Wang zhihao
     * @Date 2023/11/13 19:27
     * @Version V1.0
     */
    @Override
    public boolean removeById(String listId) {
        try {
            scriptPublishResultService.removeByListId(listId);
            baseMapper.deleteById(listId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @Method beforeQuery
     * @Param
     * @param scriptPublishList
     * @Return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hex.ds.code.detection.script.publish.entity.ScriptPublishListEntity>
     * @Description 搜索前置
     * @Author Wang zhihao
     * @Date 2023/11/10 13:34
     * @Version V1.0
     */
    private LambdaQueryWrapper<ScriptPublishListEntity> beforeQuery(ScriptPublishListEntity scriptPublishList) {
        LambdaQueryWrapper<ScriptPublishListEntity> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(scriptPublishList.getScriptPublishTaskId())) {
            wrapper.eq(ScriptPublishListEntity::getScriptPublishTaskId, scriptPublishList.getScriptPublishTaskId());
        }
        if (StrUtil.isNotBlank(scriptPublishList.getClusterId())) {
            wrapper.eq(ScriptPublishListEntity::getClusterId, scriptPublishList.getClusterId());
        }
        if (StrUtil.isNotBlank(scriptPublishList.getPublishType())) {
            wrapper.eq(ScriptPublishListEntity::getPublishType, scriptPublishList.getPublishType());
        }
        if (StrUtil.isNotBlank(scriptPublishList.getState())) {
            wrapper.eq(ScriptPublishListEntity::getState, scriptPublishList.getState());
        }
        if (StrUtil.isNotBlank(scriptPublishList.getFileName())) {
            wrapper.like(ScriptPublishListEntity::getFileName, scriptPublishList.getFileName());
        }
        wrapper.orderByDesc(ScriptPublishListEntity::getPublishType).orderByDesc(ScriptPublishListEntity::getId);
        return wrapper;
    }

}
